<template>
  <el-upload
    v-show="false"
    ref="upload"
    class="upload-demo"
    action="/api/shipping/seamap/import"
    :file-list="fileList"
    :show-file-list="false"
    :accept="accept"
    :before-upload="beforeUpload"
  >
  </el-upload>
</template>

<script>
export default {
  props: {
    query: {
      type: Object,
      default: () => {},
    },
    uploadFn: {
      default: () => {},
    },
    accept: {
      type: String,
      default: ".txt",
    },
  },
  data() {
    return {
      fileList: [],
      loading: false,
    };
  },
  methods: {
    upload() {
      this.$refs.upload.$children[0].$refs.input.click();
    },
    beforeUpload(file) {
      let fd = new FormData();
      fd.append("file", file, "fileName");
      this.loading = true;
      this.uploadFn(fd, this.query)
        .then(() => {
          this.$message.success("上传成功！");
          this.$emit("upload_success");
        })
        .catch(() => {
          this.loading = false;
        });
      return false;
    },
  },
};
</script>

<style></style>
